package ge.telasi.tasks.model.validation;

import ge.telasi.tasks.TaskException;
import ge.telasi.tasks.controller.AppConfigController;
import ge.telasi.tasks.controller.UserController;
import ge.telasi.tasks.model.Structure;
import ge.telasi.tasks.model.User;
import java.util.regex.Pattern;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;

/**
 * @author dimitri
 */
public class UserValidator extends Validator {
    public static final String USERNAME_PATTERN = "[a-zA-Z][a-zA-Z0-9_]*";

    public UserValidator() {
        super();
    }

    public UserValidator(EntityManager em) {
        super(em);
    }

    public void validateFull_server_create(User user, String password) {
        validateFull_client_create(user, password, password);
        validateUsername_server(user.getUsername());
        validateMobile_server(user.getMobile());
        validateEmail_server(user.getEmail());
    }

    public void validateFull_server_update(User user) {
        validateFull_client_update(user);
        validateMobile_server(user.getMobile());
        validateEmail_server(user.getEmail());
    }

    public void validateFull_client_create(User user, String password, String password2) {
        validateUsername_client(user.getUsername());
        validatePasswords(password, password2);
        validateFirstName(user.getFirstName());
        validateFirstName2(user.getFirstName2());
        validateLastName(user.getLastName());
        validateLastName2(user.getLastName2());
        validateMobile_client(user.getMobile());
        validateEmail_client(user.getEmail());
        validateStructure(user.getStructure());
        validatePosition(user.getPosition());
        validateLocation(user.getLocation());
        validateWorkPhone(user.getWorkPhone());
    }

    public void validateFull_client_update(User user) {
        validateFirstName(user.getFirstName());
        validateFirstName2(user.getFirstName2());
        validateLastName(user.getLastName());
        validateLastName2(user.getLastName2());
        validateMobile_client(user.getMobile());
        validateEmail_client(user.getEmail());
        validateStructure(user.getStructure());
        validatePosition(user.getPosition());
        validateLocation(user.getLocation());
        validateWorkPhone(user.getWorkPhone());
    }

    // -- client validation

    public void validateUsername_client(String username) {
        String name = "User.username";
        String value = username;
        new EmptyFieldValidator().validate(name, value);
        new TextRangeValidator(3, 20).validate(name, value);
        // pattern matching
        if (!Pattern.matches(USERNAME_PATTERN, value)) {
            String msg = getLocaleManager().getMessage("UserValidator.illegalUsername");
            throw new TaskException(msg, name);
        }
    }

    public void validatePasswords(String password, String password2) {
        String name1 = "User.password";
        String name2 = "User.password2";
        String value1 = password;
        String value2 = password2;
        new EmptyFieldValidator().validate(name1, value1);
        new TextRangeValidator(5, 20).validate(name1, value1);
        new EmptyFieldValidator().validate(name2, value2);
        new TextRangeValidator(5, 20).validate(name2, value2);
        if (!value1.equals(value2)) {
            String msg = getLocaleManager().getMessage("UserValidator.passwordNotMatches");
            throw new TaskException(msg, name1);
        }
    }

    public void validateFirstName(String firstName) {
        String name = "User.firstName";
        String value = firstName;
        new EmptyFieldValidator().validate(name, value);
        new TextRangeValidator(-1, 40).validate(name, value);
    }

    public void validateLastName(String lastName) {
        String name = "User.lastName";
        String value = lastName;
        new EmptyFieldValidator().validate(name, value);
        new TextRangeValidator(-1, 40).validate(name, value);
    }

    public void validateFirstName2(String firstName2) {
        String name = "User.firstName2";
        String value = firstName2;
        new TextRangeValidator(-1, 40).validate(name, value);
    }

    public void validateLastName2(String lastName2) {
        String name = "User.lastName2";
        String value = lastName2;
        new TextRangeValidator(-1, 40).validate(name, value);
    }

    public void validateMobile_client(String mobile) {
        String name = "User.mobile";
        String value = mobile;
        new EmptyFieldValidator().validate(name, value);
    }

    public void validateEmail_client(String email) {
        String name = "User.email";
        String value = email;
        new EmptyFieldValidator().validate(name, value);
    }

    public void validateStructure(Structure structure) {
        String name = "User.structure";
        Object value = structure;
        new EmptyFieldValidator().validate(name, value);
    }

    public void validatePosition(String position) {
        String name = "User.position";
        String value = position;
        new EmptyFieldValidator().validate(name, value);
        new TextRangeValidator(-1, 40).validate(name, value);
    }

    public void validateLocation(String location) {
        String name = "User.location";
        String value = location;
        new EmptyFieldValidator().validate(name, value);
        new TextRangeValidator(-1, 40).validate(name, value);
    }

    public void validateWorkPhone(String phone) {
        String name = "User.workPhone";
        String value = phone;
        new EmptyFieldValidator().validate(name, value);
        new TextRangeValidator(3, 10).validate(name, value);
    }

    // -- server validation

    public void validateUsername_server(String username) {
        String name = "User.username";
        User user;
        try {
            user = new UserController().find(getEm(), username);
        } catch (NoResultException nre) {
            user = null;
        }
        if (user != null) {
            String msg = getLocaleManager().getMessage("UserValidator.userExists");
            throw new TaskException(msg, name);
        }
    }

    public void validateMobile_server(String mobile) {
        requireEM();
        String name = "User.mobile";
        String value = mobile;
        String pattern = new AppConfigController().getAppConfigValue(getEm(), "mobile.pattern");
        if (!Pattern.matches(pattern, value)) {
            String msg = getLocaleManager().getMessage("UserValidator.illegalMobile");
            throw new TaskException(msg, name);
        }
    }

    public void validateEmail_server(String email) {
        String name = "User.email";
        String value = email;
        new EmptyFieldValidator().validate(name, value);
        String pattern = new AppConfigController().getAppConfigValue(getEm(), "email.pattern");
        if (!Pattern.matches(pattern, value)) {
            String msg = getLocaleManager().getMessage("UserValidator.illegalEmail");
            throw new TaskException(msg, name);
        }

    }

}
